package com.ballmaze.controllers;

import com.ballmaze.conn.Result;
import com.ballmaze.maze.MazeLoginManager;
import com.ballmaze.maze.MazeManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
public class MazeInfoController {
    @Autowired
    MazeManager mazeManager;

    @GetMapping("/regist")
    public Result regist(HttpServletRequest request) {
        String username = request.getParameter("username");
        MazeLoginManager loginManager = mazeManager.getLoginManager();
        if (loginManager.insertPlayer(username)) {
            return Result.success("注册成功", loginManager.getIdByName(username));
        } else {
            return Result.fail("注册失败", loginManager.getIdByName(username));
        }
    }

    @GetMapping("/id")
    public Result id(HttpServletRequest request) {
        String username = request.getParameter("username");
        MazeLoginManager loginManager = mazeManager.getLoginManager();
        Integer idByName = loginManager.getIdByName(username);
        if (idByName == null) {
            return Result.fail("没有此用户", -1);
        }
        return Result.fail("用户id", idByName);
    }
}
